package com.eastmoney.tutorial.android.myapp;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;
import com.eastmoney.tutorial.android.external.WebBridge;

/**
 * Created by benny.xu on 2016-03-13.
 */
public class ThirdActivity extends Activity implements View.OnClickListener {


    private WebView wev1;
    private ProgressDialog dialog;

    private Button btnBack;
    private Button btnClose;
    private Button btnRefresh;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.third);

        btnBack = (Button) findViewById(R.id.btnBack);
        btnBack.setOnClickListener(this);
        btnClose = (Button) findViewById(R.id.btnClose);
        btnClose.setOnClickListener(this);
        btnRefresh = (Button) findViewById(R.id.btnRefresh);
        btnRefresh.setOnClickListener(this);
        wev1 = (WebView) findViewById(R.id.wev1);
        wev1.loadUrl("http://account2.eastmoney.com/d/index.html");
        //wev1.loadUrl("file:///android_asset/index.html");
        WebSettings setting = wev1.getSettings();
        setting.setJavaScriptEnabled(true);

        wev1.addJavascriptInterface(new WebBridge(this), "westmoney");

        wev1.requestFocus();


        wev1.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
            }

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    closeDialog();
                } else {
                    openDialog(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }
        });
        //强制使用webview 打开地址*/
        wev1.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                if (url.endsWith("protocols?action=startActivity")) {
                    Intent intent = new Intent(ThirdActivity.this, Dummy.class);
                    startActivity(intent);

                } else {
                    view.loadUrl(url);
                }

                return true;
            }
        });


    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == 2) {
            String result = data.getStringExtra("result");
            //Toast.makeText(this,result,Toast.LENGTH_SHORT);
            String callBack = "javascript:showLoginResult('" + result + "')";
            wev1.loadUrl(callBack);


        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        Toast.makeText(ThirdActivity.this, wev1.getUrl(), Toast.LENGTH_SHORT).show();
        if (keyCode == KeyEvent.KEYCODE_BACK) {

            if (wev1.canGoBack()) {
                wev1.goBack();
                return true;
            } else {
                System.exit(0);
            }

        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnBack:
                if (wev1.canGoBack()) {
                    Toast.makeText(ThirdActivity.this, "GO BACK", Toast.LENGTH_SHORT).show();
                    wev1.goBack();
                } else {
                    System.exit(0);
                }
                break;
            case R.id.btnRefresh:
                Toast.makeText(ThirdActivity.this, "Refresh", Toast.LENGTH_SHORT).show();
                wev1.reload();
                break;
            case R.id.btnClose:
                Toast.makeText(ThirdActivity.this, "Close...", Toast.LENGTH_SHORT).show();
                System.exit(0);
                break;
            default:
                break;


        }
    }


    private void closeDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
            dialog = null;

        }

    }

    private void openDialog(int val) {
        if (dialog == null) {
            dialog = new ProgressDialog(ThirdActivity.this);
            dialog.setTitle("loading ");
            dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            dialog.setProgress(val);
            dialog.show();
        } else {
            dialog.setProgress(val);
        }

    }

}